#include "system/timer.h"

namespace dw {
	namespace system {

//------------------------------------------------------------------------------
// Construction & destruction
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
Timer::Timer()
:	_invFrequency(0.0)
,	_start(0)
{
	reset();
}

//------------------------------------------------------------------------------
// Commands
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void Timer::reset()
{
	int64 frequency;

	if (::QueryPerformanceFrequency((LARGE_INTEGER*)&frequency))
	{
		_invFrequency = 1.0 / (float64)frequency;
		BOOL result = ::QueryPerformanceCounter((LARGE_INTEGER*)&_start);
		dwAssert(result == TRUE, "Failed to query counter");
	}
}

//------------------------------------------------------------------------------
// State
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
float64 Timer::getSeconds() const
{
	return (double)getTicks() * _invFrequency;
}

//------------------------------------------------------------------------------
float64 Timer::getMilliSeconds() const
{
	return (double)getTicks() * _invFrequency * 1000.0;
}

//------------------------------------------------------------------------------
uint64 Timer::getTicks() const
{
	int64 current;
	BOOL result;

	result = ::QueryPerformanceCounter((LARGE_INTEGER*)&current);
	dwAssert(result == TRUE, "Failed to query counter");

	return current - _start;
}

	} // namespace system
} // namespace dw
